const db = require("../core/mysql")
const moment = require("moment")
const md5 = require("md5")
const jwt = require("jwt-simple")
class AccountController {
    // 注册
    async register(request, resposne, next) {
        let insertSql = 'insert into users(u_name,u_pwd,u_sex,u_create) values(?,?,?,?)'
        let params = [
            request.body.name,
            md5(request.body.pwd+require("../config").key),
            request.body.sex==""?"男":request.body.sex,
            moment().format('YYYY-MM-DD HH:mm:ss')
        ]

        try {
            let result = await db.exec(insertSql, params)
            if (result && result.affectedRows > 0) {
                resposne.json({
                    code: 200,
                    msg: "注册成功"
                })
            } else {
                resposne.json({
                    code: 500,
                    msg: "注册失败"
                })
            }
        } catch (error) {
            resposne.json({
                code: -200,
                msg: "服务器异常",
                error
            })
        }



    }


    // 登录
    async login(request, resposne, next) {
        let loginSql = 'select u_id,u_name,u_sex,u_create from users where u_name =? and u_pwd =? AND u_status=1 ;'
        let params = [
            request.body.name,
            // md5的二次加密
            md5(request.body.pwd+require("../config").key),
        ]

        try {
            let result = await db.exec(loginSql, params)
            if (result && result.length > 0) {
                resposne.json({
                    code: 200,
                    msg: "登录成功",
                    data: result[0],
                    token: createToken(result[0])
                })
            } else {
                resposne.json({
                    code: 500,
                    msg: "登录失败"
                })
            }
        } catch (error) {
            resposne.json({
                code: 500,
                msg: "登录失败",
                error
            })
        }



        function createToken(data) {
            return jwt.encode({
                exp: Date.now() + (1000 * 60 * 60 * 24),
                info: data
            }, require("../config").tokenKey)
        }
    }
}


module.exports = new AccountController();